import { AppContext } from "../AppProvider"
import { Upload, UploadProps, message } from "antd"
import React, { useContext } from "react"

export interface FileImportProps extends UploadProps {
    refresh?: Function
}

const FileImport = ({ refresh,action, children, ...rest }: FileImportProps) => {
    const {apiPrefix, commonHeaders } = useContext(AppContext)
    const onChange = ({ file }: any) => {
        if (file.response.success) {
            message.success("导入成功");
            message.destroy('FileImport');
            refresh && refresh();
        } else {
            message.error(file.response.msg);
        }
    }
    return <Upload action={apiPrefix+action} headers={commonHeaders} onChange={onChange} beforeUpload={() => {
        message.loading({
            key: 'FileImport',
            content: 'loading...'
        })
        return true
    }} {...rest} maxCount={1} showUploadList={false} >{children}</Upload>
}
export default FileImport